package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.dk.data.R;
import com.dongdongshop.bo.AlipayQueryBO;
import com.dongdongshop.bo.AlipayTradeQueryBO;
import com.dongdongshop.config.AlipayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

@Controller
public class AlipayController {

    @Autowired
    private AlipayClient alipayClient;



    //此接口传入以下四个值
    //商户订单号
    //订单名称
    //付款金额
    //商品描述
    @RequestMapping("alipayTradePagePay")
    @ResponseBody
    public String alipayTradePagePay(String WIDout_trade_no,String WIDsubject, String WIDtotal_amount,String WIDbody){
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDout_trade_no +"\","
                + "\"total_amount\":\""+ WIDtotal_amount +"\","
                + "\"subject\":\""+ WIDsubject +"\","
                + "\"body\":\""+ WIDbody +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
        //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //输出
        return result;
    }




    //查询
    //查询传入两个参数商户订单号交易流水号
    @ResponseBody
    @RequestMapping("alipayTraeQuery")
    public R alipayTraeQuery(HttpServletRequest request, String WIDTQout_trade_no, String WIDTQtrade_no){

        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDTQout_trade_no +"\","+"\"trade_no\":\""+ WIDTQtrade_no +"\"}");
        //请求
        String result =null;
        try {
            result= alipayClient.execute(alipayRequest).getBody();
            System.out.println(result);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        AlipayQueryBO alipayQueryBO = JSONUtil.toBean(result, AlipayQueryBO.class);
//        response就是实体类
        AlipayTradeQueryBO response=alipayQueryBO.getAlipy_trade_query_response();
        return R.ok().setData(response) ;
    }




    //退款    商户订单号、支付宝交易号、退款的金额、退款的原因说明、标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传
    @RequestMapping("alipayTradeRefund")
    @ResponseBody
    public String alipayTradeRefund(HttpServletRequest request,String WIDTRout_trade_no ,String  WIDTRtrade_no,String WIDTRrefund_amount ,String WIDTRrefund_reason , String WIDTRout_request_no) throws Exception {
        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDTRout_trade_no +"\","
                + "\"trade_no\":\""+ WIDTRtrade_no +"\","
                + "\"refund_amount\":\""+ WIDTRrefund_amount +"\","
                + "\"refund_reason\":\""+ WIDTRrefund_reason +"\","
                + "\"out_request_no\":\""+ WIDTRout_request_no +"\"}");
        //请求
        String result = alipayClient.execute(alipayRequest).getBody();
        //输出
       return result;
    }


}
